#include "applicationstarter.h"
#include <QDebug>
#include <QProcess>

ApplicationStarter::ApplicationStarter(QObject *parent) :
    QObject(parent)
{
    s = false;
    process = new QProcess();
    connect(process, SIGNAL(stateChanged(QProcess::ProcessState)),
            this, SLOT(processStateChanged()));
}

void ApplicationStarter::processStateChanged() {

    if(process->state() != QProcess::NotRunning ) {
        s = true;
    }
    else {
        s = false;
    }
    startingChanged();
}

bool ApplicationStarter::starting() {
    return s;
}

void ApplicationStarter::start(QString app)
{
    qDebug() << "start app:" << app;

    //Close previous application
    if(process->state() == QProcess::Running) {
        process->kill();
        process->waitForFinished();
    }

    process->start(app);

    //If process starts, sending signal
    if(process->state() == QProcess::Starting) {
        s = true;
        startingChanged();
    }

}
